package com.jk1123.us.core.id;

import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

/**
 * @author huyoufu <https://github.com/huyoufu>
 * @TIME 2021-09-13 18:45
 * @description
 * 无序的id生成器 这里我们使用 随机号段+shuffle方式来实现
 */
public class UnOrderIdGenerator implements IdGenerator {
    private static final int CAPACITY=100000;
    /**
     * 阈值
     */
    private float threshold =0.3f;
    private ArrayBlockingQueue<Long> current=new ArrayBlockingQueue<Long>(CAPACITY);
    private ArrayBlockingQueue<Long> next=new ArrayBlockingQueue<Long>(CAPACITY);

    public UnOrderIdGenerator() {
        //初始化
        this.init();
    }
    public void init(){

    }

    @Override
    public long nextId() {
        int size = current.size();
        if (current.isEmpty()){
            current=next;
            current.clear();
            return current.poll();
        }
        if(size<CAPACITY*threshold){
            //申请一个信息
            next=new ArrayBlockingQueue<Long>(CAPACITY);
        }
        return 0;
    }
}
